package app.netant.book.xs74;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class Xs74Detail extends NetAntBase {
	
	private Xs74Entity entity;

	public Xs74Detail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(Xs74Entity entity){
		this.entity = entity;
	}
	
	public void analyseType(){
		
		Elements activePart = document.select(".box_con .con_top a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
		
	}
	
	public void analyseTitle(){
		
		Elements activePart = document.select(".box_con #maininfo #info h1");
		if(activePart.size()>0){
			String name = activePart.get(0).text();
			System.out.println("【Name】:"+name);
			this.entity.setNovelName(name);
			this.entity.setNovelFullName(name);
		}
		
	}
	
	public void analyseMemo(){
		
		Elements activePart = document.select(".box_con #maininfo #intro");
		if(activePart.size()>0){
			String memo = this.getString(activePart.get(0).text());
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
		
	}
	
	public void analyseTags(){
		
		Elements activePart = document.select(".box_con #maininfo #info p");
		for(int i=0;i<2;i++){
			String tagStr = activePart.get(i).text().trim().replaceAll(" ", "");
			int index = tagStr.indexOf("：");
			if(index > 0 && index < tagStr.length()-1){
				String tagName = tagStr.substring(0,index);
				String tagValue = tagStr.substring(index+1);
				System.out.println("【TAG】:"+tagName+"="+tagValue);
				this.entity.setTag(tagName, tagValue);
			}
		}
		
	}
	
	public void analyseHref(){
		
		Elements activePart = document.select(".box_con #maininfo #info p");
		for(int i=0;i<activePart.size();i++){
			String tagStr = activePart.get(i).text().trim();
			tagStr = tagStr.replaceAll(" ", "");

			if(tagStr.startsWith("下载")){
								
				Elements aPart = activePart.get(i).select("a");
				if(aPart.size()>0){
					String pagePath = aPart.get(0).attr("href");
					
					System.out.println("实际下载路径开始分析："+pagePath);
					
					Xs74Detail detail = new Xs74Detail(pagePath);
					String href = detail.analyseDownPath();
					
					System.out.println("【Href】:"+href);
					this.entity.setDownPath(href);
					
					System.out.println("实际下载路径分析结束："+pagePath);
					
				}
				
			}
		}
		
	}
	
	public String analyseDownPath(){
		
		Elements activePart = document.select("#TxtdownTop a");
		
		if(activePart.size()>1){
			String href = activePart.get(1).attr("href");
			return href;
		}
		
		throw new RuntimeException("获取下载路径失败！");
	}
	
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseMemo();
		this.analyseTags();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		Xs74Detail detail = new Xs74Detail("https://www.xs74.com/novel/huoyingzhidameishijia/");
		
		detail.analyseType();
		detail.analyseTitle();
		detail.analyseMemo();
		detail.analyseTags();
		detail.analyseHref();
		
	}
	
}
